%Script para testear BeamForm1
clear variables
close all

addpath(genpath('../'))


load Ecografia.mat
load wave.mat
%load ../../Data110721/DataSet1.mat

pulso=wave;

c=1500; %Velocidad de propagacion en m/s
d=(11*10^-3)/(31); %Distancia entre sensores

ResX=128;
ResZ=8192;



%---------------------------------------------
%Pre-acondicionamiento de la señal
%---------------------------------------------

%Filtrado
[B,A]=butter(10,[0.05 0.3]);
data=filter(B,A,RF3); %Introduce atraso por fase, arreglar!
data=downsample(data,2); 
pulso=downsample(wave,2);
[M,N]=size(data);

%Ecualizo la señal para todos los canales
invE=1./sqrt(sum(data.*data));
%invE=1./mean(abs(data));
invE=invE./mean(invE);
data=data.*(ones(M,1)*invE);

%---------------------------------------------
% PROCESAMIENTO
%--------------------------------------------

% [D1,X1,Z1]=BeamForming(data,d,rsf,c,0.015/1500);
% figure
% imshow(D1/max(max(D1)))
% figure
% hold on
% title('Beam Forming pasivo con redondeo')
% imagesc(X1,Z1,abs(D1)')
% hold off

%pulso=[zeros(1,N);ones(1,N)];
[D,X,Z]=BeamFormingActivo(pulso,data,d,40*10^6,40*10^6,0.015/1500,c,0.01,0.07,0,d*32,ResX,ResZ);
if sum(sum(imag(D)))~=0
    disp('Warning: el resultado es complejo')
end
figure
hold on
title('Beam Forming activo')
imagesc(X,Z,real(D)')
hold off
figure
surf(X,Z,real(D)','EdgeColor','none')

%Señal original
figure
mesh(data)
figure
imagesc(data)

%-----------------------------
%VersionBenech
%-----------------------------
% Sal = BeamFormingBruto(data,d,rsf,15);
% figure
% imagesc(Sal)   
% figure
% surf(Sal)
            
  
